<template>
  <!-- 首页的内容区域 -->
  <div class="dashboard-editor-container">
    <panel-group
      @handleSetLineChartData="handleSetLineChartData"
      :done="getDoneCount('done')"
      :undone="getDoneCount('undone')"
      :auto="getAutoCount('自动')"
      :notAuto="getAutoCount('主动')"
    />

    <!-- <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <line-chart :chart-data="lineChartData" />
    </el-row> -->

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart :data="data" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>

      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart :done="done" :undone="undone" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import PanelGroup from "./dashboard/PanelGroup";
import LineChart from "./dashboard/LineChart";
import RaddarChart from "./dashboard/RaddarChart";
import PieChart from "./dashboard/PieChart";
import BarChart from "./dashboard/BarChart";
import { selectNotAuto, selectAuto } from "@/api/system/summary";
import { done, undone } from "@/api/system/todo";

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145],
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130],
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130],
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130],
  },
};

export default {
  name: "Index",
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
  },
  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      data: [
        // { name: "自动", value: 1 },
        // { name: "主动", value: 1 },
      ], // 总结
      done: [], // 完成
      undone: [], // 未完成
    };
  },
  mounted() {
    this.NotAuto();
    this.Auto();
    this.done1();
    this.undone1();
  },
  methods: {
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type];
    },
    // 获取主动写
    NotAuto() {
      selectNotAuto()
        .then((res) => {
          this.data.push({ name: "主动", value: res.data.length });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 获取自动写
    Auto() {
      selectAuto()
        .then((res) => {
          this.data.push({ name: "自动", value: res.data.length });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 查询完成
    done1() {
      done()
        .then((res) => {
          const donelist = res.data.map((item) => {
            return item["count"];
          });
          this.done = donelist;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    //查询为完成
    undone1() {
      undone()
        .then((res) => {
          const undonelist = res.data.map((item) => {
            return item["count"];
          });
          this.undone = undonelist;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 获取完成/为完成的数量
    getDoneCount(type) {
      if (type === "done") {
        return eval(this.done.join("+"));
      }
      if (type === "undone") {
        return eval(this.undone.join("+"));
      }
    },
    // 获取自动/手动数量
    getAutoCount(type) {
      var res = 0;
      for (var i = 0; i < this.data.length; i++) {
        if (this.data[i].name === type) {
          res = this.data[i].value;
        }
        // break;
      }
      return res;
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
